package com.uusee.crawler.framework;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.uusee.crawler.model.CrawlURI;

public class CrawlController {
	protected Log log = LogFactory.getLog(CrawlController.class);
	
	private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4, 0, TimeUnit.SECONDS,
			new ArrayBlockingQueue(1000), new ThreadPoolExecutor.CallerRunsPolicy());
	
    private StopWatch stopWatch = new StopWatch();
	
	public CrawlController() {
		stopWatch.start();
	}  
	public void assignTask(Job job,CrawlURI crawlURI) {
		try {
			CrawlThread crawlThread = new CrawlThread(crawlURI, job);
			threadPoolExecutor.execute(crawlThread);
			log.info("分配任务：" + crawlURI.getCrawlUrl()+",总任务数："+threadPoolExecutor.getTaskCount());
			
			long taskCount = threadPoolExecutor.getTaskCount();
			if (taskCount%500 == 0) {
				while(true) {
					try {
						Thread.sleep(10000);
					}catch (InterruptedException e) {
					}
					if (threadPoolExecutor.getActiveCount() == 0) {
						break;
					}
					log.info("任务数：" + threadPoolExecutor.getCompletedTaskCount()+"/"+threadPoolExecutor.getTaskCount()+",用时：" + stopWatch.getTime() / 1000 + "秒。");
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
			log.error(e);
		}
	}

	public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
		this.threadPoolExecutor = threadPoolExecutor;
	}
	
}
